package com.zmn.oms.api.listener;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.ccb.common.consts.CCBCallConst;
import com.zmn.ccc.common.consts.MQConst;
import com.zmn.ccc.common.model.record.CdrRecordDRO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.mediadetection.CallSyncDetectionBService;
import com.zmn.oms.model.bo.mediadetection.DetectionBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：呼叫cdr事件监听
 *
 * @author ligang
 * @date 2022/08/09 11:44
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.cc}")
@Component
public class CdrEventListener {

    @Resource
    private CallSyncDetectionBService callSyncDetectionBService;

    /**
     * 需处理的外呼业务类型
     */
    private List<Integer> outBusinessTypeCodeList = Lists.newArrayList(CCBCallConst.OUT_BIZ_TYPE_ORDER_DETAIL,
            CCBCallConst.OUT_BIZ_TYPE_REASSIGNMENT_REASON);

    /**
     * 通话记录
     *
     * @param message
     */
    @AliyunMQHandler(tags = MQConst.RECORD_CDR_TAG)
    public void cdrRecord(String message) {
        try {
            CdrRecordDRO cdrRecordDRO = JSON.parseObject(message, CdrRecordDRO.class);
            if (!outBusinessTypeCodeList.contains(cdrRecordDRO.getOutBusinessTypeCode())) {
                return;
            }
            log.info("#CdrEventListener#cdrRecord receive message:{}", message);
            DetectionBO detectionBO = BeanMapper.map(cdrRecordDRO, DetectionBO.class);
            callSyncDetectionBService.syncDetection(detectionBO);
        } catch (Exception e) {
            log.error("#CdrEventListener#cdrRecord receive 异常:{}", e.getMessage(), e);
        }
    }
}
